package com.zelix.analy;

import com.zelix.tool.AnalyTool;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;

import java.io.IOException;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

public class RemoveAnnotations {
    public static void main(String[] args) throws IOException {
        JarFile jarFile = new JarFile("G:\\license-server-installer\\web\\WEB-INF\\lib\\lsrv-floating.jar");
        ZipEntry entry = jarFile.getEntry("com/jetbrains/ls/floating/f.class");
        if (entry != null) {
            ClassReader cr = new ClassReader(jarFile.getInputStream(entry));
            ClassNode cn = new ClassNode();
            cr.accept(cn, 0);

            //去除类的注解
            if (cn.visibleAnnotations != null && cn.visibleAnnotations.size() > 0) {
                cn.visibleAnnotations.clear();
            }

            //去除属性的注解
            cn.fields.stream().forEach(f -> {
                if (f.invisibleAnnotations != null && f.invisibleAnnotations.size() > 0) {
                    f.invisibleAnnotations.clear();
                }
            });


            //写入文件
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            cn.accept(cw);
            AnalyTool.classToFile("f.class", cw.toByteArray());
        }
    }
}
